package com.renli.service.impl;

import com.renli.bean.ResultJson;
import com.renli.mapper.RoleMapper;
import com.renli.pojo.Role;
import com.renli.pojo.RolePerm;
import com.renli.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper mapper;

    @Override
    public List<Role> findByLoginId(Integer loginId) {
        return mapper.selectByLoginId(loginId);
    }

    @Override
    public ResultJson<List<Role>> find(Map<String, Object> map) {

        if (map.get("currentPage") == null || map.get("currentPage").equals("")) {
            map.put("currentPage", 1);
        }
        if (map.get("pageSize") == null || map.get("pageSize").equals("")) {
            map.put("pageSize", 6);
        }


        Integer start = (Integer) map.get("pageSize") * ((Integer) map.get("currentPage") - 1);
        map.put("start", start);

        List<Role> roleList = mapper.findAll(map);
        Long total = mapper.findAllCount(map);
        ResultJson<List<Role>> resultJson = new ResultJson<>();
        resultJson.setSuccess("查询成功", roleList, total);

        return resultJson;


    }

    @Override
    public ResultJson updatePerm(Map<String, Integer> listMap) {

        if (mapper.updatePerm(listMap) > 0) {
            return new ResultJson().setSuccess("修改成功");
        } else {
            return new ResultJson().setSuccess("修改失败");
        }
    }

    @Override
    public ResultJson addPerm(Map<String, Integer> map) {

        if (mapper.addPerm(map)>0){
            return new ResultJson().setSuccess("增加成功");
        }else {
            return new ResultJson().setSuccess("增加失败");
        }
    }

    @Override
    public ResultJson delPerm(Map<String, Integer> map) {

        if (mapper.delPerm(map)>0){
            return new ResultJson().setSuccess("删除成功");
        }else {
            return new ResultJson().setSuccess("删除失败");
        }
    }
}
